﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmNoiNhan : Form
    {
        public frmNoiNhan()
        {
            InitializeComponent();
        }
        private void LoadHeThongcbn()
        {
            cbnHeThongThem.DisplayMember = "Ten";
            cbnHeThongThem.ValueMember = "ID";

            cbnHeThongSua.DisplayMember = "Ten";
            cbnHeThongSua.ValueMember = "ID";

            cbnHeThongMoi.DisplayMember = "Ten";
            cbnHeThongMoi.ValueMember = "ID";

            List<HeThongDTO> tmp = HeThongBUS.LoadAll();
            cbnHeThongThem.DataSource = tmp;
            cbnHeThongSua.DataSource = new List<HeThongDTO>(tmp);
            cbnHeThongMoi.DataSource = new List<HeThongDTO>(tmp);
        }
        private void LoadNoiNhanlst()
        {
            lstDanhSachNoiNhan.DisplayMember = "Ten";
            lstDanhSachNoiNhan.ValueMember = "ID";
            lstDanhSachNoiNhan.DataSource = NoiNhanBUS.LoadAll(((int)(cbnHeThongSua.SelectedValue)), false);
        }
        private void frmNoiNhan_Load(object sender, EventArgs e)
        {
            btnThem.Enabled = false;
            btnLuu.Enabled = false;
            LoadHeThongcbn();
            lstDanhSachNoiNhan.SelectedIndex = -1;
            cbnHeThongMoi.SelectedIndex = -1;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                HeThongDTO tmpHeThong = new HeThongDTO();
                tmpHeThong = (HeThongDTO)cbnHeThongThem.Items[cbnHeThongThem.SelectedIndex];

                NoiNhanDTO tmpNoiNhan = new NoiNhanDTO();
                tmpNoiNhan.Ten = txtNoiNhanMoi.Text.Trim();
                tmpNoiNhan.HeThong = tmpHeThong;

                if (NoiNhanBUS.Add(tmpNoiNhan))
                {
                    txtNoiNhanMoi.Text = "";
                    btnThem.Enabled = false;

                    for (int i = 0; i < cbnHeThongSua.Items.Count; i++)
                    {
                        if (((HeThongDTO)(cbnHeThongSua.Items[i])).ID == tmpHeThong.ID)
                        {
                            cbnHeThongSua.SelectedIndex = i;
                            break;
                        }
                    }

                    LoadNoiNhanlst();
                    lstDanhSachNoiNhan.SelectedIndex = -1;

                }
                else
                {
                    MessageBox.Show("Không thể thêm nơi nhận mới.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void txtNoiNhanMoi_TextChanged(object sender, EventArgs e)
        {
            if (txtNoiNhanMoi.Text.Trim() == "")
            {
                btnThem.Enabled = false;
            }
            else
            {
                btnThem.Enabled = true;
            }
        }

        private void cbnHeThongSua_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbnHeThongSua.SelectedIndex == -1)
            {
                lstDanhSachNoiNhan.SelectedIndex = -1;
                return;
            }
            LoadNoiNhanlst();
            lstDanhSachNoiNhan.SelectedIndex = -1;
        }

        private void txtNoiNhanSua_TextChanged(object sender, EventArgs e)
        {
            if (txtNoiNhanSua.Text.Trim() == "")
            {
                btnLuu.Enabled = false;
            }
            else
            {
                btnLuu.Enabled = true;
            }
        }

        private void lstDanhSachNoiNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstDanhSachNoiNhan.SelectedIndex == -1)
            {
                txtNoiNhanSua.Text = "";
                txtNoiNhanSua.Enabled = false;
                btnLuu.Enabled = false;
                cbnHeThongMoi.SelectedIndex = -1;
                cbnHeThongMoi.Enabled = false;
                return;
            }
            txtNoiNhanSua.Text = ((NoiNhanDTO)(lstDanhSachNoiNhan.SelectedItem)).Ten;
            txtNoiNhanSua.Enabled = true;
            btnLuu.Enabled = true;
            cbnHeThongMoi.Enabled = true;
            int tmp = ((NoiNhanDTO)(lstDanhSachNoiNhan.SelectedItem)).HeThong.ID;
            for (int i = 0; i < cbnHeThongMoi.Items.Count; i++)
            {
                if (((HeThongDTO)(cbnHeThongMoi.Items[i])).ID == tmp)
                {
                    cbnHeThongMoi.SelectedIndex = i;
                    break;
                }
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                HeThongDTO tmpHeThong = new HeThongDTO();
                tmpHeThong = (HeThongDTO)cbnHeThongMoi.Items[cbnHeThongMoi.SelectedIndex];

                NoiNhanDTO tmpNoiNhan = new NoiNhanDTO();
                tmpNoiNhan = (NoiNhanDTO)lstDanhSachNoiNhan.Items[lstDanhSachNoiNhan.SelectedIndex];
                tmpNoiNhan.Ten = txtNoiNhanSua.Text.Trim();
                tmpNoiNhan.HeThong = tmpHeThong;

                if (NoiNhanBUS.Edit(tmpNoiNhan))
                {
                    txtNoiNhanSua.Text = "";
                    LoadNoiNhanlst();
                    lstDanhSachNoiNhan.SelectedIndex = -1;
                }
                else
                {
                    MessageBox.Show("Không thể sửa thông tin noi nhận.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

    }
}
